#include "Brick.h"


Brick::Brick():MObject()
{
	
}

Brick::Brick(float x, float y, int deltaTime, int resourceName,CResourceManager *rm):MObject(x,y,0,0,deltaTime,resourceName,rm)
{
	_objectType = CResourceManager::TYPE_BRICK;
	_isMovable = false;
	_lastX = _x;
	_lastY = _y;
	this->_isAnimating = false;
	_status = STATUS_NORMAL;
	_willBreaked = false;


	_partial[0] = rm->getSprite(CResourceManager::BRICKEXP);
	for (int i=1;i<4;i++)
		_partial[i] = _partial[0];
	
}

Brick::~Brick(void)
{
	 
}

void Brick::Render()
{
	switch (_status)
	{
	case STATUS_NORMAL:
	case STATUS_SLIDE:
		{
			MObject::Render();
			break;
		}
	case STATUS_BREAKED:
		{
			Vector v = CScreen::getInstance()->getPosition();
			_partial[0]->Render(_p1.getX() - v.getX(),_p1.getY() - v.getY());
			_partial[1]->Render(_x + 2*(_x-_p1.getX()) - v.getX(),_p1.getY() - v.getY());
			_partial[2]->Render(_p2.getX() - v.getX(),_p2.getY());
			_partial[3]->Render(_x + 2*(_x-_p2.getX()) - v.getX(),_p2.getY() - v.getY());

			break;
		}
	}
}

void Brick::actAfterCollided(int Direction, shared_ptr<MObject> Object)
{
	switch (Direction)
	{
	case 1: //UP
		{
			break;
		}
	case 2:	//DOWN
	case 3:	//RIGHT
	case 4: //LEFT
		break;
	}
}

void Brick::setStatus(int Status)
{
	_status = Status;
	switch(Status)
	{
	case STATUS_NORMAL:
		{
			_x = _lastX;
			_y = _lastY;
			_vy = 0;
			this->_isAnimating = false;

			break;
		}
	case STATUS_SLIDE:
		{
			_vy = -.5f;
			this->_isAnimating = true;
			//_lastX = _x;
			//_lastY = _y;
			break;
		}
	case STATUS_SLIDE_BREAKED:
		{
			_willBreaked = true;
			setStatus(STATUS_SLIDE);
			break;
		}
	case STATUS_BREAKED:
		{
			this->_isAnimating = true;
			_v1 = Vector(-0.3f,-1.2f);
			_p1 = Vector(_x,_y + 16);
			_v2 = Vector(-0.5f,-1.5f);
			_p2 = Vector(_x,_y);
			CMarioSound::getInstance()->Play(CMarioSound::SOUND_BRICKBROKEN,0);
			break;
		}
	case STATUS_DELETED:
		{
			this->_isAnimating = false;
			break;
		}
	}
}

void Brick::Animate()
{
	switch (_status)
	{
	case STATUS_NORMAL:
		{
			
			break;
		}
	case STATUS_SLIDE:
		{
			_vy += .1f;
				_y += _vy*_deltaTime;
			if (_willBreaked)
				setStatus(STATUS_BREAKED);
			if (_y >= _lastY)
				setStatus(STATUS_NORMAL);
			break;
		}
	case STATUS_BREAKED:
		{
			_p1.setX(_p1.getX() + _v1.getX()*_deltaTime);
			_p1.setY(_p1.getY() + _v1.getY()*_deltaTime);

			_v1.setY(_v1.getY() + 0.15f);

			_p2.setX(_p2.getX() + _v2.getX()*_deltaTime);
			_p2.setY(_p2.getY() + _v2.getY()*_deltaTime);

			_v2.setY(_v2.getY() + 0.15f);

			if (_p1.getY() > 600 && _p2.getY() > 600)
				setStatus(STATUS_DELETED);
			break;

		}
	}
}